<?php
namespace app\admin\controller;


use app\admin\code\BaseCode;
use app\admin\logic\BaseLogic;
use app\admin\util\Page;
use app\admin\util\Response;
use app\admin\validate\BaseValidate;
use think\Model;
use think\Request;

class Common extends Base
{
    /**
     * @var Model
     */
    protected $_model;
    /**
     * @var BaseValidate
     */
    protected $_validate;
    /**
     * @var BaseLogic
     */
    protected $_logic;
    public function __construct(Request $request = null)
    {
        $action = get_called_class();
        $this->_validate = str_replace('controller','validate',$action).'Validate';
        $this->_model = str_replace('controller','model',$action).'Model';
        $this->_logic = str_replace('controller','logic',$action).'Logic';
        parent::__construct($request);
    }
    /**
     * 列表页
     */
    public function index(){
        if($this->_ajax){
            $model = new $this->_model;
            $order = Request::instance()->param('order');
            $where = $this->get_where();
            $list = $model->lists($where,$order);
            $list = $this->filter_list($list);
            Response::json(BaseCode::SUCCESS,'',Page::return_page_data($list));
        }else{
            $this->index_assign();
            return view();
        }
    }

    /**
     * 删除动作
     */
    public function delete(){
        $param = Request::instance()->param();
        $logic = new $this->_logic($param);
        if($logic->del(new $this->_validate())){
            Response::json(BaseCode::SUCCESS);
        }else{
            Response::json($logic->err['code'],$logic->err['msg']);
        }
    }

    /**
     * 添加动作
     */
    public function add(){
        if($this->_ajax){
            $param = Request::instance()->param();
            $logic = new $this->_logic($param);
            if($logic->add(new $this->_validate())){
                Response::json(BaseCode::SUCCESS);
            }else{
                Response::json($logic->err['code'],$logic->err['msg']);
            }
        }else{
            $this->assign('act',url('add'));
            $this->add_assign();
            return view('oppt');
        }
    }

    /**
     * 详情页
     */
    public function detail(){
        $id = Request::instance()->param('id');
        $model = new $this->_model;
        $vo = $model->findData(['id'=>$id]);
        $this->update_assign();
        $this->assign('vo',$vo);
        $this->assign('act',url('update'));
        return view('oppt');
    }


    /**
     * 更新页
     */
    public function update(){
        $param = Request::instance()->param();
        $logic = new $this->_logic($param);
        $logic->update(new $this->_validate());
        Response::json(BaseCode::SUCCESS);
    }

    /**
     * 过滤列表，如进行数据转换的操作
     * @param $list
     * @return mixed
     */
    protected function filter_list($list)
    {
        return $list;
    }
    /**
     * 列表页面额外输出
     */
    protected function index_assign()
    {
    }
    /**
     * 添加页面的额外输出
     */
    protected function add_assign()
    {
    }
    /**
     * 更新页面的额外输出
     */
    protected function update_assign()
    {
    }
    /**
     * 获取搜索条件
     */
    protected function get_where(){
        return [];
    }
}
